package cn.stylefeng.guns.modular.hx.storage.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.core.util.QRCodeUtil;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.hx.storage.entity.HxStoragePutDetailed;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoragePutDetailedParam;
import cn.stylefeng.guns.modular.hx.storage.model.params.HxStoragePutParam;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStoragePutDetailedResult;
import cn.stylefeng.guns.modular.hx.storage.model.result.HxStoragePutResult;
import cn.stylefeng.guns.modular.hx.storage.service.HxStoragePutDetailedService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 入库单明细表控制器
 *
 * @author zhucx
 * @Date 2023-08-14 10:54:09
 */
@Controller
@RequestMapping("/hxStoragePutDetailed")
public class HxStoragePutDetailedController extends BaseController {

    private String PREFIX = "/hx/storage/hxStoragePutDetailed";

    private String PREFIX2 = "/hx/storage/hxInventoryStatistics";

    @Autowired
    private HxStoragePutDetailedService hxStoragePutDetailedService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;
    
    /**
     * 去打印标签页面
     */
    @RequestMapping("/tobtnqrCode")
    public String tobtnqrCode() {
        return PREFIX + "/btnqrCode.html";
    }

    /**
     * 跳转到入库质检页面
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/hxStoragePutDetailed.html";
    }

    /**
     * 跳转到入库记录页面
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("putjilu")
    public String putjilu(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择客户
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/hxStoragePutDetailed_jilu.html";
    }

    /**
     * 新增页面
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxStoragePutDetailed_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxStoragePutDetailed_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxStoragePutDetailedParam hxStoragePutDetailedParam) {
        this.hxStoragePutDetailedService.add(hxStoragePutDetailedParam);
        return ResponseData.success();
    }




    @ResponseBody
    @RequestMapping("/listAll")
    public List <HxStoragePutDetailedResult> listAll(HxStoragePutDetailedParam hxStoragePutDetailedParam , String condition,String startTime,String endTime) {
        User user = userService.getcurrentUser();
        hxStoragePutDetailedParam.setDeptId(user.getDeptId());
        hxStoragePutDetailedParam.setOperateId(user.getOperateId());
        if(ToolUtil.isEmpty(hxStoragePutDetailedParam.getTimeType())) {
            hxStoragePutDetailedParam.setTimeType("zj");
        }
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStoragePutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        return this.hxStoragePutDetailedService.findlistAllBySpec(hxStoragePutDetailedParam,condition,user.getOperateId());
    }
    @ResponseBody
    @RequestMapping("/listAll1")
    public List <HxStoragePutDetailedResult> listAll1(HxStoragePutDetailedParam hxStoragePutDetailedParam , String condition,String startTime,String endTime) {
        User user = userService.getcurrentUser();
        hxStoragePutDetailedParam.setDeptId(user.getDeptId());
        hxStoragePutDetailedParam.setOperateId(user.getOperateId());
        if(ToolUtil.isEmpty(hxStoragePutDetailedParam.getTimeType())) {
            hxStoragePutDetailedParam.setTimeType("sj");
        }
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStoragePutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        return this.hxStoragePutDetailedService.listAll1(hxStoragePutDetailedParam,condition,user.getOperateId());
    }
    /**
     * 编辑接口
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxStoragePutDetailedParam hxStoragePutDetailedParam) {
        this.hxStoragePutDetailedService.update(hxStoragePutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 更新打印次数接口
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/btnEditPrint")
    @ResponseBody
    public ResponseData btnEditPrint(Integer[] ids) {
    	for (Integer id : ids) {
    		HxStoragePutDetailedParam hxStoragePutDetailedParam = new HxStoragePutDetailedParam();
    		hxStoragePutDetailedParam.setId(id);
    		this.hxStoragePutDetailedService.updatePrint(hxStoragePutDetailedParam);
		}
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxStoragePutDetailedParam hxStoragePutDetailedParam) {
        this.hxStoragePutDetailedService.delete(hxStoragePutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxStoragePutDetailedParam hxStoragePutDetailedParam) {
        HxStoragePutDetailed detail = this.hxStoragePutDetailedService.getById(hxStoragePutDetailedParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查看详情接口
     *
     * @author zhucx
     * @Date 2023-08-14
     */
    @RequestMapping("/details")
    @ResponseBody
    public ResponseData details(HxStoragePutDetailedParam hxStoragePutDetailedParam) {
    	HxStoragePutDetailedResult detail = this.hxStoragePutDetailedService.details(hxStoragePutDetailedParam);
        return ResponseData.success(detail);
    }

    /**
     * 查询入库记录列表
     * @author zhucx
     * @Date 2023-08-14
     */
    @ResponseBody
    @RequestMapping("/jilulist")
    public LayuiPageInfo jilulist(HxStoragePutDetailedParam hxStoragePutDetailedParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	hxStoragePutDetailedParam.setDeptId(user.getDeptId());
    	hxStoragePutDetailedParam.setOperateId(user.getOperateId());
    	if(ToolUtil.isEmpty(hxStoragePutDetailedParam.getTimeType())) {
    		hxStoragePutDetailedParam.setTimeType("sj");
    	}
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			hxStoragePutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
		}
        return this.hxStoragePutDetailedService.findjiluPageBySpec(hxStoragePutDetailedParam,condition);
    }

    /**
     * 查询列表
     * @author zhucx
     * @Date 2023-08-14
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxStoragePutDetailedParam hxStoragePutDetailedParam,String condition ) {
        return this.hxStoragePutDetailedService.findPageBySpec(hxStoragePutDetailedParam,condition);
    }

    /**
     * 查询
     * 质检列表
     * @author zhucx
     * @Date 2023-08-14
     */
    @ResponseBody
    @RequestMapping("/qualitylist")
    public LayuiPageInfo qualitylist(HxStoragePutDetailedParam hxStoragePutDetailedParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	hxStoragePutDetailedParam.setDeptId(user.getDeptId());
    	hxStoragePutDetailedParam.setOperateId(user.getOperateId());
    	if(ToolUtil.isEmpty(hxStoragePutDetailedParam.getTimeType())) {
    		hxStoragePutDetailedParam.setTimeType("zj");
    	}
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			hxStoragePutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
		}
        return this.hxStoragePutDetailedService.findqualityPageBySpec(hxStoragePutDetailedParam,condition);
    }

    @ResponseBody
    @RequestMapping("/count")
    public ResponseData count(HxStoragePutDetailedParam hxStoragePutDetailedParam,String condition ,String startTime,String endTime) {
        User user = userService.getcurrentUser();
        hxStoragePutDetailedParam.setDeptId(user.getDeptId());
        hxStoragePutDetailedParam.setDeliverId(user.getDeliverId());
        if(ToolUtil.isEmpty(hxStoragePutDetailedParam.getTimeType())) {
            hxStoragePutDetailedParam.setTimeType("cj");
        }
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStoragePutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        return ResponseData.success(this.hxStoragePutDetailedService.count(hxStoragePutDetailedParam,condition,user.getOperateId()));

    }
    @ResponseBody
    @RequestMapping("/count1")
    public ResponseData count1(HxStoragePutDetailedParam hxStoragePutDetailedParam,String condition ,String startTime,String endTime) {
        User user = userService.getcurrentUser();
        hxStoragePutDetailedParam.setDeptId(user.getDeptId());
        hxStoragePutDetailedParam.setDeliverId(user.getDeliverId());
        if(ToolUtil.isEmpty(hxStoragePutDetailedParam.getTimeType())) {
            hxStoragePutDetailedParam.setTimeType("cj");
        }
        if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
            hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        } else if (startTime == null && endTime == null){
            hxStoragePutDetailedParam.setTimeInterval("'"+DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        return ResponseData.success(this.hxStoragePutDetailedService.count1(hxStoragePutDetailedParam,condition,user.getOperateId()));

    }
    /**
     * 创建二维码
     */	
    @RequestMapping(value = "/getqrcode", method = RequestMethod.GET)
    @ResponseBody
    public void generateQRCode4Product(String id,String name, HttpServletRequest request, HttpServletResponse response) {
        try {
            //logo
            File logoFile = new File("");
            String url = id;
            QRCodeUtil.drawLogoQRCode(logoFile, url, name,170,150,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建条码
     */	
    @RequestMapping(value = "/getCode_128", method = RequestMethod.GET)
    @ResponseBody
    public void generateCode_1284Product(String id,String name, HttpServletRequest request, HttpServletResponse response) {
        try {
            //logo
            File logoFile = new File("");
            String url = id;
            QRCodeUtil.drawLogoCode_128(logoFile, url, name,260,67,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("/hxInventoryStatistics")
    public String hxInventoryStatistics() {
        return PREFIX2 + "/hxInventoryStatistics.html";
    }

    /**
     * 入库统计
     * warehousingstat
     */
    @ResponseBody
    @RequestMapping("warehousingstat")
    public LayuiPageInfo warehousingstat(HxStoragePutDetailedParam hxStoragePutDetailedParam, String condition, String startTime, String endTime){
        if ( ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)){
            hxStoragePutDetailedParam.setTimeInterval("'"+startTime+"' and '"+endTime+"'");
        }else if (startTime == null && endTime == null){
            hxStoragePutDetailedParam.setTimeInterval("'"+ DateUtil.getDate()+"' and '"+DateUtil.getDate()+"'");
        }
        hxStoragePutDetailedParam.setPutState("WAREHOUSED");
        return this.hxStoragePutDetailedService.findwarehousingstat(hxStoragePutDetailedParam,condition);
    };
}
